import type { TimeFilter, KeyString, KeySortable, KeyBigInt, KeyBool, KeyAccumulatable, ConditionMatchInteger, ConditionMatchIntegerOrs } from "qqlx-core";
import type {
  ConditionMatchStr,
  ConditionMatchStrOrs,
  ConditionMatchBool,
  ConditionMatchEnum,
  ConditionMatchEnumOrs,
  ConditionRegExpStr,
  ConditionSort,
  ConditionTime,
} from "qqlx-core";
import { ConditionType, Sortable } from "qqlx-core";

import { toString } from "@lib/com.verify";

// ======================================================================================= 1

export function getConditionMatchStr<T>(key: KeyString<T>, value: string = "", isMatch: boolean = true): ConditionMatchStr<T> {
  return {
    type: ConditionType.MatchStr,
    key,
    value,
    isMatch,
  };
}

/** 数据查询前，会进行去重 */
export function getConditionMatchStrOrs<T>(key: KeyString<T>, value: string[] = [], isMatch: boolean = true): ConditionMatchStrOrs<T> {
  return {
    type: ConditionType.MatchStrOrs,
    key,
    value,
    isMatch,
  };
}

// ======================================================================================= 2

export function getConditionMatchBool<T>(key: KeyBool<T>, value: boolean = false): ConditionMatchBool<T> {
  return {
    type: ConditionType.MatchBool,
    key,
    value,
  };
}

// ======================================================================================= 3

export function getConditionMatchEnum<T>(key: KeyAccumulatable<T>, value: number = -1, isMatch: boolean = true): ConditionMatchEnum<T> {
  return {
    type: ConditionType.MatchEnum,
    key,
    value,
    isMatch,
  };
}

/** 数据查询前，会进行去重 */
export function getConditionMatchEnumOrs<T>(key: KeyAccumulatable<T>, value: number[] = [], isMatch: boolean = true): ConditionMatchEnumOrs<T> {
  return {
    type: ConditionType.MatchEnumOrs,
    key,
    value,
    isMatch,
  };
}

// ======================================================================================= 3

export function getConditionMatchInteger<T>(key: KeyAccumulatable<T>, value: number = -1, isMatch: boolean = true): ConditionMatchInteger<T> {
  return {
    type: ConditionType.MatchInteger,
    key,
    value,
    isMatch,
  };
}

/** 数据查询前，会进行去重 */
export function getConditionMatchIntegerOrs<T>(key: KeyAccumulatable<T>, value: number[] = [], isMatch: boolean = true): ConditionMatchIntegerOrs<T> {
  return {
    type: ConditionType.MatchIntegerOrs,
    key,
    value,
    isMatch,
  };
}

// ======================================================================================= 4

export function getConditionRegExpStr<T>(key: KeyString<T>, value: string = "", isMatch: boolean = true): ConditionRegExpStr<T> {
  return {
    type: ConditionType.RegExpStr,
    key,
    value,
    isMatch,
  };
}

// ======================================================================================= sort

export function getConditionSort<T>(key: KeySortable<T>, value: Sortable = Sortable.DESC, isMatch: boolean = true): ConditionSort<T> {
  return {
    type: ConditionType.Sort,
    key,
    value,
    isMatch,
  };
}

// ======================================================================================= time

/** 默认设置 0~当前时间 */
export function getTimeFilter(): TimeFilter {
  return {
    startTime: "0",
    endTime: toString(Date.now()),
  };
}

/** 默认设置 0~当前时间 */
export function getConditionTime<T>(key: KeyBigInt<T>, isMatch: boolean = true): ConditionTime<T> {
  return {
    type: ConditionType.Time,
    key: key,
    value: getTimeFilter(),
    isMatch,
  };
}
